import sys
import pygame
import time
from . import ezmenu
from . import config
from .Player import MakePlayer
from .splash import Splash
from .splash import RoundDisplay
from .splash import WinDisplay
from .splash import WinnerScreen

#Fighters
from .mrgreen import MrGreen
from .mrred import MrRed
#########

Fighters = [
    MrRed(),
    MrGreen()
]


def ExitGame():
    pygame.quit()
    sys.exit()


class Sinif(object):

    main_menu = True
    set_menu = False
    p1_menu = False
    p2_menu = False
    Karakter1 = None
    Karakter2 = None
    battle_backr = None
    battle = False
    winner = False
    winnerf = 0
    battle_id = 0
    round_id = 1
    round_frame = 0
    winner_frame = 0

pygame.init()
pygame.display.set_caption("PyFI 0.5")

dspi = pygame.display.Info()

tw = dspi.current_w
th = dspi.current_h

if not config.full_screen:
    config.x = (tw / 4) * 3
    config.y = (th / 4) * 3
    pencere = pygame.display.set_mode([config.x, config.y])

else:
    config.x = tw
    config.y = th
    pencere = pygame.display.set_mode([tw, th])
    pygame.display.toggle_fullscreen()
    pygame.mouse.set_visible(False)

menu_background = pygame.image.load(config.main_menu_backr)
menu_banner = pygame.image.load(config.main_menu_banner)
pygame.mixer.music.load(config.main_menu_song)
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play()
saat = pygame.time.Clock()

#SPLASH SCREEN
time.sleep(0.5)

#if config.full_screen:
    #pygame.display.toggle_fullscreen()

Splash(pencere)


def SetHumanP1(params):
    Sinif.p1_menu = False
    Sinif.p2_menu = True
    Sinif.Karakter1 = MakePlayer(params, "LEFT")


def SetHumanP2(params):
    Sinif.p1_menu = False
    Sinif.p2_menu = False
    Sinif.battle = True
    Sinif.round_frame = 0
    Sinif.winner_frame = 0
    Sinif.winnerf = 0
    Sinif.Karakter2 = MakePlayer(params, "RIGHT")
    Sinif.round_limit = config.num_of_rounds
    SetBattleBackr()


def NextRound():

    Sinif.Karakter1.check_win()
    Sinif.Karakter2.check_win()

    p1_wins = bool(Sinif.Karakter1.nwins == config.num_of_rounds)
    p2_wins = bool(Sinif.Karakter2.nwins == config.num_of_rounds)

    if p1_wins or p2_wins:
        if p1_wins:
            name = Sinif.Karakter1.name
        else:
            name = Sinif.Karakter2.name

        WinnerScreen(pencere, name)
        Sinif.round_id = 1
        Sinif.main_menu = True
        Sinif.battle = False
    else:
        Sinif.round_frame = 0
        Sinif.winner_frame = 0
        Sinif.winnerf = 0
        Sinif.round_id += 1
        Sinif.Karakter1.reset()
        Sinif.Karakter2.reset()

    if not (p1_wins or p2_wins) and Sinif.round_limit == Sinif.round_id:
        Sinif.round_limit += 1


def SetBattleBackr():

    if Sinif.battle_id > len(config.battle_backrs):
        Sinif.battle_id = 1

    Sinif.battle_backr = pygame.image.load(
        config.battle_backrs[Sinif.battle_id - 1]
    )


def NewOneVsOne():
    Sinif.main_menu = False
    Sinif.p1_menu = True
    Sinif.battle_id += 1


def NoMusic():
    pygame.mixer.music.pause()


def Music():
    pygame.mixer.music.unpause()


def Settings():

    if Sinif.main_menu:
        Sinif.main_menu = False
        pencere.fill((0, 0, 255))
        pygame.display.flip()
        Sinif.set_menu = True
    elif not Sinif.main_menu:
        Sinif.main_menu = True
        pencere.fill((0, 0, 255))
        pygame.display.flip()
        Sinif.set_menu = False


def CheckCollide():

    if Sinif.Karakter1.can <= 0:
        Sinif.winnerf = 2
        Sinif.Karakter2.win = True
        Sinif.Karakter1.win = False
        Sinif.Karakter1.KO = True
        Sinif.Karakter2.KO = True
    elif Sinif.Karakter2.can <= 0:
        Sinif.winnerf = 1
        Sinif.Karakter1.win = True
        Sinif.Karakter2.win = False
        Sinif.Karakter1.KO = True
        Sinif.Karakter2.KO = True
    else:
        Sinif.Karakter1.x_mov = True
        Sinif.Karakter2.x_mov = True

        if Sinif.Karakter1.x() > Sinif.Karakter2.x():
            Sinif.Karakter1.ynr = False
            Sinif.Karakter2.ynr = False
        if Sinif.Karakter1.x() < Sinif.Karakter2.x():
            Sinif.Karakter1.ynr = True
            Sinif.Karakter2.ynr = True

        if pygame.sprite.collide_rect(Sinif.Karakter1, Sinif.Karakter2):
            a = Sinif.Karakter1.y() + Sinif.Karakter1.h
            b = Sinif.Karakter2.y() + Sinif.Karakter2.h
            if a == b:
                Sinif.Karakter1.x_mov = False
                Sinif.Karakter2.x_mov = False
            else:
                Sinif.Karakter1.x_mov = True
                Sinif.Karakter2.x_mov = True

            if Sinif.Karakter1.Atak and not Sinif.Karakter2.in_def:
                Sinif.Karakter2.eksican(Sinif.Karakter1.saldiri1)
                Sinif.Karakter2.under_atk = True

            elif Sinif.Karakter2.Atak and not Sinif.Karakter1.in_def:
                Sinif.Karakter1.eksican(Sinif.Karakter2.saldiri1)
                Sinif.Karakter1.under_atk = True
            else:
                Sinif.Karakter1.under_atk = False
                Sinif.Karakter2.under_atk = False


def main():
    menu = ezmenu.EzMenu([
        ["1 vs 1", NewOneVsOne],
        ["Settings", Settings],
        ["Exit", ExitGame]])
    menu.position(config.x / 2, (config.y / 2))
    menu.set_font(pygame.font.Font("font/Bleeding Cowboys.ttf", 64))
    menu.set_highlight_color(config.hcolor_option)
    menu.set_normal_color(config.color_option)
    menu3 = ezmenu.EzMenu([
        ["Music On", Music],
        ["Music Of", NoMusic],
        ["Back", Settings]])
    menu3.position(config.x / 2, (config.y / 2))
    menu3.set_font(pygame.font.Font("font/Bleeding Cowboys.ttf", 64))
    menu3.set_highlight_color(config.hcolor_option)
    menu3.set_normal_color(config.color_option)
    #make menu ------------
    pre_menu = []
    for Fighter in Fighters:
        pre_menu.append([
                Fighter.name,
                SetHumanP1,
                Fighter.params()
            ])

    menu4 = ezmenu.EzMenu(pre_menu)
    menu4.position(100, 10)
    menu4.set_font(pygame.font.Font("font/Bleeding Cowboys.ttf", 64))
    menu4.set_highlight_color(config.hcolor_option)
    menu4.set_normal_color(config.color_option)

    pre_menu = []
    for Fighter in Fighters:
        pre_menu.append([
                Fighter.name,
                SetHumanP2,
                Fighter.params()
            ])

    menu5 = ezmenu.EzMenu(pre_menu)
    menu5.position(100, 10)
    menu5.set_font(pygame.font.Font("font/Bleeding Cowboys.ttf", 64))
    menu5.set_highlight_color(config.hcolor_option)
    menu5.set_normal_color(config.color_option)

    while 1:

        events = pygame.event.get()

        for event in events:
            if event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_ESCAPE):
                    #ResetResolution()
                    #pygame.display.toggle_fullscreen()
                    pygame.quit()
                    return

            if event.type == pygame.QUIT:
                pygame.quit()
                return

        pencere.fill((0, 0, 0))
        pencere.blit(menu_background, (0, 0))

        if Sinif.main_menu:

            #pencere.fill((0, 0, 255))
            pencere.blit(menu_background, (0, 0))
            pencere.blit(menu_banner, ((config.x / 2), (config.y / 4)))
            menu.durum = False
            menu.update(events)
            menu.draw(pencere)
            menu3.durum = True

        else:

            if Sinif.p1_menu:
                menu4.draw(pencere)
                menu4.update(events)

            elif Sinif.p2_menu:
                menu5.draw(pencere)
                menu5.update(events)

            elif Sinif.battle:

                    #pencere.fill((0, 0, 255))
                    #Can Barlari
                    pencere.blit(Sinif.battle_backr, (0, 0))
                    pencere.blit(Sinif.Karakter1.profil, (15, 25))
                    pencere.blit(Sinif.Karakter2.profil, (config.x - 115, 25))

                    pencere.blit(Sinif.Karakter2.image, Sinif.Karakter2.rect)

                    Sinif.Karakter1.update()

                    pygame.draw.rect(
                        pencere,
                        config.life_bar_color,
                        (125, 25, Sinif.Karakter1.can, 15),
                        0
                    )

                    pencere.blit(Sinif.Karakter1.image, Sinif.Karakter1.rect)

                    Sinif.Karakter2.update()

                    caniki = Sinif.Karakter2.can - Sinif.Karakter2.can * 2

                    pygame.draw.rect(
                        pencere,
                        config.life_bar_color,
                        (config.x - 125, 25, caniki, 15),
                        0
                    )

                    if Sinif.round_frame <= 20:
                        RoundDisplay(
                            pencere,
                            Sinif.round_id,
                            Sinif.round_frame
                        )
                        Sinif.round_frame += 2

                    if not Sinif.winnerf == 0:
                        WinDisplay(pencere, Sinif.winnerf)

                        if Sinif.winner_frame <= 20:
                            Sinif.winner_frame += 1
                        else:
                                NextRound()

                    CheckCollide()

            if Sinif.set_menu:
                menu.durum = True
                menu3.durum = False
                #pencere.fill((0, 0, 255))
                pencere.blit(menu_background, (0, 0))
                menu3.draw(pencere)
                menu3.update(events)
                pygame.display.flip()

        pygame.display.flip()
        saat.tick(14)

if __name__ == "__main__":
    main()
